
import sys,os,time
import socket

SDVOS_READING_LIGHT=200

unity_model_ip = 'localhost'

def doorOpen():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos2
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":1,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    #Gate control,open door
    for idx in range(0,100):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":201,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()

def doorClose():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos2
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":1,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    for idx in range(100,0,-1):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":201,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()

def seatPositionForward():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Postion, Pos3
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":2,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    #Driver Seat Control
    for idx in range(0,100):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":202,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()

def seatPositionBackward():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Postion, Pos3
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":2,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    for idx in range(100,0,-1):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":202,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()


def setAmbientLight(color):
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos5
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":5,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    #Control AtmosphereLamp
    client.send(b'{"_obj_id":"Cube","_op":204,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (color))
    time.sleep(0.5)
    client.close()

def setMarqueeMode():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos5
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":5,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    #Control AtmosphereLamp
    for idx in range(0, 10):
        print("IDX:",idx)
        client.send(b'{"_obj_id":"Cube","_op":204,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (idx))
        time.sleep(0.5)
    client.close()

def setFrontLightOn():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos1
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":0,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    client.send(b'{"_obj_id":"Cube","_op":206,"_cmd":90,"x":0,"y":0,"z":0}\n')
    client.close()

def setFrontLightOff():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos1
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":0,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    client.send(b'{"_obj_id":"Cube","_op":206,"_cmd":0,"x":0,"y":0,"z":0}\n')
    client.close()

def controlWiper():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos1
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":0,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    client.send(b'{"_obj_id":"Cube","_op":203,"_cmd":0,"x":0,"y":0,"z":0}\n')
    client.close()

def setBrakeLightOn():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos4
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":4,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    client.send(b'{"_obj_id":"Cube","_op":207,"_cmd":90,"x":0,"y":0,"z":0}\n')
    client.close()

def setBrakeLightOff():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos4
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":4,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    client.send(b'{"_obj_id":"Cube","_op":207,"_cmd":0,"x":0,"y":0,"z":0}\n')
    client.close()

def setDomeLightOn():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos5
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":2,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    for idx in range(0,30):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":200,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()

def setDomeLightOff():
    client= socket.socket()
    client.connect((unity_model_ip, 5555))
    #Switch Camera Position, Pos5
    client.send(b'{"_obj_id":"Cube","_op":205,"_cmd":2,"x":0,"y":0,"z":0}\n')
    time.sleep(3.0)
    for idx in range(30,0,-1):
        tar_y_pos = idx
        client.send(b'{"_obj_id":"Cube","_op":200,"_cmd":%d,"x":0,"y":0,"z":0}\n' % (tar_y_pos))
        time.sleep(0.01)
        #print(tar_y_pos)
    client.close()

if __name__ == "__main__":
    doorClose()

